package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryWarnMonthDao;
import com.boss.salary.entity.SalaryWarnMonth;
import com.boss.sys.ca.user.dto.UserDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class SalaryWarnMonthDaoImpl extends BaseDaoImpl implements ISalaryWarnMonthDao {
    @Override
    public void updateSalaryWarnMonth(SalaryWarnMonth salaryWarnMonth, UserDTO userDTO) {
        StringBuilder sql = new StringBuilder();
        sql.append(" merge  into ").append(TableName.SALARY_WARN_MONTH)
                .append(" a using " )
                .append( " (select  ? id, ? create_time,? update_time, ? warn_month,? mof_div_code from dual ) b " )
                .append(" on (a.id = b.id) " )
                .append(" WHEN MATCHED THEN " )
                .append("   UPDATE SET a.update_time = b.update_time,a.warn_month=b.warn_month " )
                .append( " WHEN NOT MATCHED THEN " )
                .append( "   INSERT " )
                .append( "    (a.id, a.create_time, a.update_time, a.warn_month,a.mof_div_code) ")
                .append( "   VALUES " )
                .append("     (b.id, b.create_time, b.update_time, b.warn_month,b.mof_div_code)");

        Object[] params = new Object[]{
                salaryWarnMonth.getId(), salaryWarnMonth.getCreateTime(), salaryWarnMonth.getUpdateTime(), salaryWarnMonth.getWarnMonth(),userDTO.getProvince()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public SalaryWarnMonth getSalaryWarnMonth(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select id,create_time, update_time, warn_month from ").append(TableName.SALARY_WARN_MONTH);
        List<Object> paramList = new ArrayList<>();
        if (!StringUtils.isEmpty(province)) {
            sql.append(" where mof_div_code = ?");
            paramList.add(province);
        }
        try {
            return this.getjdbcTemplate().queryForObject(sql.toString(), new BeanPropertyRowMapper<>(SalaryWarnMonth.class), paramList.toArray());
        }catch (Exception exception){
            SalaryWarnMonth swm= new SalaryWarnMonth();
            swm.setWarnMonth("0");
            return swm;
        }
    }
}
